package com.hankol.newretail.adapter.cart

import android.annotation.SuppressLint
import android.graphics.Paint
import android.util.Log
import android.view.View
import android.view.View.OnAttachStateChangeListener
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.core.view.doOnDetach
import androidx.recyclerview.widget.RecyclerView
import cn.iwgang.countdownview.CountdownView
import com.baselibrary.Constants
import com.baselibrary.extension.checkNotNull
import com.baselibrary.extension.setMallPriceTTF
import com.baselibrary.router.ActivityRouter
import com.baselibrary.utils.*
import com.baselibrary.view.RoundCornerImageView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.hankol.newretail.R
import com.hankol.newretail.bean.ShopCartBean
import com.hankol.newretail.callback.OnShopCartCallBack
import com.hankol.newretail.adapter.GoodsGiftCartAdapter
import com.hankol.newretail.bean.ShopCartListBean
import java.util.HashMap

/**
 * @author: 关忆北、
 * @date: 2022/8/5
 * @Des：单个商品
 */
class ShopCartChildAdapter : BaseQuickAdapter<ShopCartListBean, BaseViewHolder>(R.layout.item_shop_cart_child) {
    private var onShopCartCallBack: OnShopCartCallBack? = null
    fun setOnShopCartCallBack(onShopCartCallBack: OnShopCartCallBack?) {
        this.onShopCartCallBack = onShopCartCallBack
    }

    @SuppressLint("SetTextI18n")
    override fun convert(helper: BaseViewHolder, item: ShopCartListBean) {
        helper.addOnClickListener(R.id.ll_shop_cart_det)//删除
        helper.addOnClickListener(R.id.ll_shop_cart_reduce)//商品数量-1
        helper.addOnClickListener(R.id.ll_shop_cart_plus)//商品数量+1
        helper.addOnClickListener(R.id.ll_cart_child_select)//选中 or 取消选中


        GlideUtils.loadImg(mContext.applicationContext, item.skuCover, helper.getView<RoundCornerImageView>(R.id.round_cart_child_cover))

        helper.setText(R.id.tv_cart_child_label, SpecUtils.getNormsStr4ShopCart(item.specList)).setText(R.id.tv_cart_child_name, item.spuName).setText(R.id.tv_cart_child_limit_number, item.buyPolicyText).setText(R.id.ll_shop_cart_num, item.num.toString()).setText(R.id.tv_cart_child_price, "${AmountUtil.changeF2YKeepTwo(item.price.toString())}").setGone(R.id.tv_cart_child_limit_number, item.buyPolicyText.checkNotNull())
        helper.getView<TextView>(R.id.tv_cart_child_price).setMallPriceTTF(mContext)
        // helper.getView<TextView>(R.id.tv_cart_child_price_sign).setMallPriceTTF(mContext)
        if (item.num > 1) {
            helper.getView<ImageView>(R.id.iv_shop_cart_reduce).setImageResource(R.drawable.ic_goods_reduce_true)
        } else {
            helper.getView<ImageView>(R.id.iv_shop_cart_reduce).setImageResource(R.drawable.ic_goods_reduce_false)
        }

        if (item.num >= item.maxNum) {
            helper.getView<ImageView>(R.id.iv_shop_cart_plus).setImageResource(R.drawable.ic_goods_add_false)
        } else {
            helper.getView<ImageView>(R.id.iv_shop_cart_plus).setImageResource(R.drawable.ic_goods_add_true)
        }


        // 0正常，1已下架，2无库存，3限制购买
        when (item.status) {
            0 -> {
                //正常
                helper.addOnClickListener(R.id.ll_cart_child_goods)
                helper.addOnClickListener(R.id.round_cart_child_cover)
                helper.setGone(R.id.ll_cart_num_action, true)
                helper.setGone(R.id.tv_cart_child_limit_tip, false)
                helper.setGone(R.id.view_cart_child_limit_tip, false)
                helper.getView<LinearLayout>(R.id.ll_cart_child_select).isEnabled = true

                if (item.selected == 1) {
                    helper.getView<LinearLayout>(R.id.ll_cart_child_select).isSelected = true
                    helper.setImageResource(R.id.iv_cart_child_select, R.drawable.ic_shopcart_selected)
                } else {
                    helper.getView<LinearLayout>(R.id.ll_cart_child_select).isSelected = false
                    helper.setImageResource(R.id.iv_cart_child_select, R.drawable.ic_shopcart_unselected)
                }
                onShopCartCallBack?.onComputePriceCall()
            }
            1 -> {
                //1已下架
                helper.setGone(R.id.ll_cart_num_action, false)
                helper.setGone(R.id.tv_cart_child_limit_tip, true)
                helper.setGone(R.id.view_cart_child_limit_tip, true)
                helper.setText(R.id.tv_cart_child_limit_tip, "已下架")
                helper.getView<LinearLayout>(R.id.ll_cart_child_select).isEnabled = false
                helper.setImageResource(R.id.iv_cart_child_select, R.drawable.ic_shopcart_limit)
            }
            2 -> {
                //2无库存
                helper.setGone(R.id.ll_cart_num_action, false)
                helper.setGone(R.id.tv_cart_child_limit_tip, true)
                helper.setGone(R.id.view_cart_child_limit_tip, true)
                helper.setText(R.id.tv_cart_child_limit_tip, "无库存")
                helper.getView<LinearLayout>(R.id.ll_cart_child_select).isEnabled = false
                helper.setImageResource(R.id.iv_cart_child_select, R.drawable.ic_shopcart_limit)
            }
            3 -> {
                //3限制购买
                helper.setGone(R.id.ll_cart_num_action, false)
                helper.setGone(R.id.tv_cart_child_limit_tip, true)
                helper.setGone(R.id.view_cart_child_limit_tip, true)
                helper.setText(R.id.tv_cart_child_limit_tip, "限制购买")
                helper.getView<LinearLayout>(R.id.ll_cart_child_select).isEnabled = false
                helper.setImageResource(R.id.iv_cart_child_select, R.drawable.ic_shopcart_limit)
            }
        }
        if (!item.mallProductGiftCommodityVos.isNullOrEmpty()) {
            helper.setGone(R.id.ll_gift, true)
            val goodsGiftCartAdapter = GoodsGiftCartAdapter()
            helper.getView<RecyclerView>(R.id.rv_gift_list).adapter = goodsGiftCartAdapter
            goodsGiftCartAdapter.setNewData(item.mallProductGiftCommodityVos)
        } else {
            helper.setGone(R.id.ll_gift, false)
        }

        if (item.seckillActProductDTO.checkNotNull()) {
            helper.setGone(R.id.ll_seckill, true)
            var startTime = item.seckillActProductDTO.stime
            var endTime = item.seckillActProductDTO.etime
            var seckillPrice = if (SpUtils.getInstance().isFans) AmountUtil.changeF2YKeepTwo(item.seckillActProductDTO.fansPrice.toString()) else AmountUtil.changeF2YKeepTwo(item.seckillActProductDTO.price.toString())
            if (System.currentTimeMillis() < startTime) {
                helper.setGone(R.id.iv_seckill_logo, false)
                helper.setImageResource(R.id.iv_seckill_label, R.drawable.ic_seckill_pre)
                helper.setText(R.id.tv_seckill_start_time, "${TimeUtil.getMonthDayTime(startTime)}")
                helper.setText(R.id.tv_seckill_price, "秒杀价¥$seckillPrice")
                helper.setGone(R.id.tv_seckill_left_time, false)
                helper.setGone(R.id.tv_seckill_start_time, true)
            } else if (System.currentTimeMillis() in startTime..endTime) {
                helper.setGone(R.id.iv_seckill_logo, true)
                helper.setImageResource(R.id.iv_seckill_label, R.drawable.ic_cart_seckill)
                helper.setGone(R.id.tv_seckill_start_time, false)
                helper.setText(R.id.tv_seckill_price, "限时秒杀中，距结束还剩")
                helper.setText(R.id.tv_cart_child_price, seckillPrice)
                helper.setGone(R.id.tv_seckill_left_time, true)
                var timer = helper.getView<CountdownView>(R.id.tv_seckill_left_time)
                timer.start(endTime - System.currentTimeMillis())
                timer.addOnAttachStateChangeListener(object : OnAttachStateChangeListener {
                    override fun onViewAttachedToWindow(v: View?) {
                        timer.start(endTime - System.currentTimeMillis())
                    }

                    override fun onViewDetachedFromWindow(v: View?) {
                    }

                })
                timer.setOnCountdownEndListener {
                    notifyItemChanged(helper.adapterPosition)
                }
            }
            helper.getView<RelativeLayout>(R.id.ll_seckill).setOnClickListener {
                if (!DoubleClickHelper.isOnDoubleClick()) {
                    val map = HashMap<String, Any>()
                    map[Constants.GOODS_DETAIL_ID] = item.spuId
                    map[Constants.GOODS_SKU_ID] = item.skuId
                    map[Constants.GOODS_DETAIL_SOURCE] = "购物车"
                    ARouterUtils.jump(ActivityRouter.NEW_RETAIL.GOODS_DETAUILS, map)
                }
            }
        } else {
            helper.setGone(R.id.ll_seckill, false)
            helper.setGone(R.id.iv_seckill_logo, false)
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    fun selectedAll() {
        data.forEach { it ->
            if (it.status == 0) {
                it.selected = 1
            }
        }
        notifyDataSetChanged()
    }

    @SuppressLint("NotifyDataSetChanged")
    fun unSelectedAll() {
        data.forEach { it ->
            if (it.status == 0) {
                it.selected = 0
            }
        }
        notifyDataSetChanged()
    }
}